import Vue from 'vue'
import Router from 'vue-router'

import Home from '@/view/Home'
import PageNotFound from '@/view/404'
import Login from '@/view/Login'

import LeftSide from '@/components/goods/leftSide'
import goodsLibrary from '@/components/goods/goodsLibrary'
import storesGoods from '@/components/goods/storesGoods'
import onlineStore from '@/components/goods/onlineStore'
import inventoryQuery from '@/components/goods/inventoryQuery'
import procurementOrder from '@/components/goods/procurementOrder'
import goodsOutlib from '@/components/goods/goodsOutlib'
import goodsInlib from '@/components/goods/goodsInlib'
import chuku from '@/components/goods/chuku'
import libcontent from '@/components/goods/library/libcontent'
import libtype from '@/components/goods/library/libtype'

import zhu from '@/components/summarize/zhu'

import assetsSider from '@/components/assets/assetsSider'

import customerSider from '@/components/customer/customerSider'

import dataSider from '@/components/data/dataSider'
import dataGeneralsituation from '@/components/data/dataGeneralsituation'

import OnlineStoreSider from '@/components/OnlineStore/OnlineStoreSider'

import toolSider from '@/components/tool/toolSider'
import storeInfo from '@/components/tool/storeInfo/storeInfo'



Vue.use(Router)

const router =  new Router({
  routes: [
        
         { path: '/login',name:'Login',component:Login },
         {path: '/',redirect:'/login'},
        {
            path: '/home',
            component:Home ,
            redirect:'/summarize/zhu',
            children:[
                {
                    path: '/summarize/zhu',
                    component: resolve => require(['../components/summarize/zhu.vue'], resolve),
                    meta:{
                        title:"xxx-概述信息",
                        requireAuth:true //判断此是是否要登录
                    }
                },
                {
                    path: '/goods/leftSide',
                    component: resolve => require(['../components/goods/leftSide.vue'], resolve),
                    children:[
                        {
                            path: '/goods/goodsLibrary',
                            component: resolve => require(['../components/goods/goodsLibrary.vue'], resolve),
                            
                            children:[
                                {
                                    path: '/goods/library/libcontent',
                                    component: resolve => require(['../components/goods/library/libcontent.vue'], resolve),
                                    meta:{
                                        title:'xxx-商品管理',
                                    }
                                },
                                {
                                    path: '/goods/library/libtype',
                                    component: resolve => require(['../components/goods/library/libtype.vue'], resolve),
                                },
                            ]
                        },
                        {
                            path: '/goods/chuku',
                            component: resolve => require(['../components/goods/chuku.vue'], resolve),
                            meta:{
                                requireAuth:true
                            }
                        },
                        {
                            path: '/goods/storesGoods',
                            component: resolve => require(['../components/goods/storesGoods.vue'], resolve),
                            
                        },
                        {
                            path: '/goods/onlineStore',
                            component: resolve => require(['../components/goods/onlineStore.vue'], resolve),
                            
                        },
                        {
                            path: '/goods/inventoryQuery',
                            component: resolve => require(['../components/goods/inventoryQuery.vue'], resolve),
                            
                        },
                        {
                            path: '/goods/procurementOrder',
                            component: resolve => require(['../components/goods/procurementOrder.vue'], resolve),
                            
                        },
                        {
                            path: '/goods/goodsOutlib',
                            component: resolve => require(['../components/goods/goodsOutlib.vue'], resolve),
                            
                        },
                        {
                            path: '/goods/goodsInlib',
                            component: resolve => require(['../components/goods/goodsInlib.vue'], resolve),
                            
                        },       
                    ]
                },
                {
                    path: '/TheOrder/orderSider',
                    component: resolve => require(['../components/TheOrder/orderSider.vue'], resolve),
                    meta:{
                        title:"xxx-订单中心",
                    }
                },
                {
                    path: '/assets/assetsSider',
                    component: resolve => require(['../components/assets/assetsSider.vue'], resolve),
                    meta:{
                        title:"xxx-资产中心",
                    }
                },
                {
                    path: '/customer/customerSider',
                    component: resolve => require(['../components/customer/customerSider.vue'], resolve),
                    meta:{
                        title:"xxx-客户中心",
                    }
                },
                {
                    path: '/data/dataSider',
                    component: resolve => require(['../components/data/dataSider.vue'], resolve),
                    children:[
                        {
                            path: '/data/dataGeneralsituation',
                            component: resolve => require(['../components/data/dataGeneralsituation.vue'], resolve),
                            meta:{
                                title:"xxx-数据管理",
                            },
                        },
                    ]
                },
                {
                    path: '/OnlineStore/OnlineStoreSider',
                    component: resolve => require(['../components/OnlineStore/OnlineStoreSider.vue'], resolve),
                    meta:{
                        title:"xxx-网店管理",
                    }
                },
                {
                    path: '/tool/toolSider',
                    component: resolve => require(['../components/tool/toolSider.vue'], resolve),
                    meta:{
                        title:"xxx-设置中心",
                    },
                    children:[
                        {
                            path: '/tool/storeInfo/storeInfo',
                            component: resolve => require(['../components/tool/storeInfo/storeInfo.vue'], resolve),
                        },
                    ]
                },
               
            ]
        },  
        {
            path: '*',
            component: resolve => require(['../view/404.vue'], resolve),
        },             
   ]
})

// 路由拦截
 
 router.beforeEach((to, from, next) => {
    if(to.path==='/login' || to.path==='*'){
        next()
    }else{
    if(to.meta.title){
        document.title =to.meta.title
    }
    if (to.matched.some(res => res.meta.requireAuth)) { //检测是否有requireAuth
        if (sessionStorage.getItem('token')) {
            next();
        } else {
            next({
            path: '/login', // 未登录则跳转至login页面
            query: {redirect: to.fullPath} // 登陆成功后回到当前页面，这里传值给login页面，to.fullPath为当前点击的页面
            });
        }
    } else {
        next();
        }
  }

});
  

export default  router


